package com.muyan.mq.core.config;


import com.muyan.mq.core.properties.MqProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.amqp.SimpleRabbitListenerContainerFactoryConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * bean 的name 不能和方法名称一样否则无法自动创建队列
 *
 * @author lrc
 * @version 1.0
 * @date 2023/3/09
 * @description
 */
@Configuration
@EnableRabbit
@Slf4j
public class RabbitConfig {


    @Autowired
    private MqProperties travelMqProperties;



    /**
     * 生产者MQ 连接工厂
     */
    @Bean(name = "mqConnectionFactory")
    public ConnectionFactory dayeConnectionFactory() {
        return getConnectionFactory(travelMqProperties.getMqUrl(), travelMqProperties.getPort(), travelMqProperties.getUserName(), travelMqProperties.getPasswd(), travelMqProperties.getVirtualHost());
    }

    /**
     * 生产者发送消息
     */
    @Bean(name = "mqProductRabbitTemplate")
    public RabbitTemplate dayeProductRabbitTemplate(@Qualifier("mqConnectionFactory") ConnectionFactory connectionFactory) {
        return new RabbitTemplate(connectionFactory);
    }


    /**
     * MQ 消费者
     */
    @Bean(name = "mqConsumerListenerFactory")
    public SimpleRabbitListenerContainerFactory dayeListenerFactory(SimpleRabbitListenerContainerFactoryConfigurer configurer, @Qualifier("mqConnectionFactory") ConnectionFactory connectionFactory) {
        //设置手动ack
        SimpleRabbitListenerContainerFactory listenerContainerFactory = new SimpleRabbitListenerContainerFactory();
        listenerContainerFactory.setAcknowledgeMode(AcknowledgeMode.MANUAL);
        //listenerContainerFactory.setConcurrentConsumers(2);
        listenerContainerFactory.setPrefetchCount(50);
        configurer.configure(listenerContainerFactory, connectionFactory);
        return listenerContainerFactory;
    }



    private ConnectionFactory getConnectionFactory(String url, Integer port, String username, String password, String virtualHost) {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
        connectionFactory.setHost(url);
        connectionFactory.setPort(port);
        connectionFactory.setUsername(username);
        connectionFactory.setPassword(password);
        connectionFactory.setVirtualHost(virtualHost);
        return connectionFactory;
    }


    /**
     * 交换机
     * @return
     */
    @Bean(name = "fanoutExchange")
    public FanoutExchange fanoutExchange() {
        boolean durable = false;
        boolean autoDelete = false;
        return new FanoutExchange(travelMqProperties.getExchangeName(), durable, autoDelete);
    }



 /*   *//**
     * 创建保存本地队列
     * bean的名称不能和方法名一致否则不会自动创建队列
     * @return
     *//*
    @Bean(name = "saveApiQueue")
    public Queue saveDb() {
        // 是否持久化
        boolean durable = false;
        // 仅创建者可以使用的私有队列，断开后自动删除
        boolean exclusive = false;
        // 当所有消费客户端连接断开后，是否自动删除队列
        boolean autoDelete = false;


        return new Queue(MqConstant.QUEE_ASK_LEAVE, durable, exclusive, autoDelete);
    }



    *//**
     * API队列绑定到交换机
     * @param saveApiQueue
     * @param fanoutExchange
     * @return
     *//*
    @Bean
    public Binding saveDbBinding(@Qualifier("saveApiQueue")Queue saveApiQueue, FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(saveApiQueue)
                .to(fanoutExchange);
    }
*/



    //创建 host 创建用户名密码
    /**
     * API队列绑定到交换机
     * @param saveApiQueue
     * @param fanoutExchange
     * @return
     */
   /* @Bean
    public Binding saveDbBinding(@Qualifier("saveApiQueue")Queue saveApiQueue, FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(saveApiQueue)
                .to(fanoutExchange);
    }*/
}
